from sklearn import svm
import pandas as pd
import numpy as np
from feature_function import show_accuracy, Euclidean_distance, Euclidean_distance11, Angle, get_dist_feature
from visible_function import tSNE, MDS, RandomForest
import joblib

# ----------------参数设置
expand_num = 5

# -------------------------------------------------准备训练数据---------------------------------------------------------
# label
train_filename = 'imagesnew/'
# filename='test_images/'
train_resultname = 'result_' + train_filename[:-1]
train_csv = train_resultname + '.csv'
print('训练数据为{}'.format(train_csv))
# train_csvname = 'result_imagesnew.csv'
train_label = pd.read_csv(train_csv, usecols=[0])
train_label = np.array(train_label).T
y_train_label = train_label[0]

# data 可取指定特征
usecols1 = []
usecols2 = []
usecols3 = []
for i in range(33):
    usecols1 += ['x_pose' + str(i), 'y_pose' + str(i)]
for i in range(21):
    usecols2 += ['x_righthand' + str(i), 'y_righthand' + str(i)]
for i in range(21):
    usecols3 += ['x_lefthand' + str(i), 'y_lefthand' + str(i)]
usecols = usecols1

# data
data = pd.read_csv(train_csv, usecols=usecols1)
pose_data = np.array(data)
data = pd.read_csv(train_csv, usecols=usecols2)
righthand_data = np.array(data)
data = pd.read_csv(train_csv, usecols=usecols3)
lefthand_data = np.array(data)

train_dist_feature = []
for i in range(len(y_train_label)):
    # print(i)
    x_pose = pose_data[i]
    x_righthand = righthand_data[i]
    x_lefthand = lefthand_data[i]
    if x_righthand[0] != 0 and x_lefthand[0] != 0:
        left_right_dist = expand_num * Euclidean_distance11(x_lefthand, 4, x_righthand, 8)
    else:
        left_right_dist = 0

    # print('{}th nose_finger_dist={}'.format(i,nose_finger_dist))
    if x_lefthand[0] != 0:
        nose_finger_dist = 5 * Euclidean_distance11(x_pose, 0, x_lefthand, 8)
    elif x_righthand[0] != 0:
        nose_finger_dist = 5 * Euclidean_distance11(x_pose, 0, x_righthand, 8)
    else:
        nose_finger_dist = 0

    dist_feature_i = get_dist_feature(x_pose, x_righthand, x_lefthand)

    train_dist_feature.append(dist_feature_i)
x_train_data = np.array(train_dist_feature)
print('x_train_data.shape={}'.format(x_train_data.shape))

# -------------------------------------------------准备测试数据------------------------------------------------------------
test_filename = 'test_images/'
test_resultname = 'result_' + test_filename[:-1]
test_csv = test_resultname + '.csv'
print('测试数据为{}'.format(test_csv))

test_label = pd.read_csv(test_csv, usecols=[0])
test_label = np.array(test_label).T
y_test_label = test_label[0]

# data
test_data = pd.read_csv(test_csv, usecols=usecols1)
test_pose_data = np.array(test_data)
test_data = pd.read_csv(test_csv, usecols=usecols2)
test_righthand_data = np.array(test_data)
test_data = pd.read_csv(test_csv, usecols=usecols3)
test_lefthand_data = np.array(test_data)

test_dist_feature = []
for i in range(len(y_test_label)):
    x_pose = test_pose_data[i]
    x_righthand = test_righthand_data[i]
    x_lefthand = test_lefthand_data[i]
    dist_feature_i = get_dist_feature(x_pose, x_righthand, x_lefthand)
    test_dist_feature.append(dist_feature_i)
x_test_data = np.array(test_dist_feature)
print('x_test_data.shape={}'.format(x_test_data.shape))

# distance0,distance1,distance2,distance3,distance4,distance5,distance6=[],[],[],[],[],[],[]
# for i in range(len(y_label)):
# # for i in range(1):
#     x=x_data[i]
#     distance0.append(Euclidean_distance(x,0,16))
#     distance1.append(Euclidean_distance(x,0,15))
#     distance2.append(Euclidean_distance(x,15,16))
#     distance3.append(Euclidean_distance(x,16,12))
#     distance4.append(Euclidean_distance(x,11,15))
#     distance5.append(Euclidean_distance(x,16,24))
#     distance6.append(Euclidean_distance(x,15,23))
# df = pd.DataFrame({'distance0':distance0,'distance1':distance1,'distance2':distance2
#                           ,'distance3':distance3,'distance4':distance4,'distance5':distance5
#                              ,'distance6':distance6})
# df.to_csv('result.csv', mode='a', index=False, header=False, sep=',')

# 训练
# ------------------------------------------------------训练数据---------------------------------------------------------
x_train = x_train_data
y_train = y_train_label
clf = svm.SVC(decision_function_shape='ovo')
model = clf.fit(x_train, y_train)
y_train_hat = model.predict(x_train)
print(y_train, '\n', y_train_hat)
show_accuracy(y_hat=y_train_hat, y_train=y_train, type='训练集')

# ----------------------------------------------------测试集结果------------------------------------------------------------
joblib.dump(model, 'model_.pkl')
model_ = joblib.load('model_.pkl')

x_test = x_test_data
y_test = y_test_label
y_test_hat = model_.predict(x_test)
print('x_test.shape={}'.format(x_test.shape))
print(y_test, '\n', y_test_hat)
show_accuracy(y_hat=y_test_hat, y_train=y_test, type='测试集')

# 降维可视化
# 用MDS方法可视化测试集数据分布、预测结果分布
MDS(x_train, y_train, "x")
# 用tSNE方法
tSNE(x_train, y_train, "x")
# 用RandomForest方法
RandomForest(x_train, y_train, "x")
